﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DeliveryService
{
    public class BLGoods
    {
        public List<MGoods> GetGoodsByBrandAndSmallCategory(string brandName, string smallCategory)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.Goods.OrderBy(s => s.Goods_Price.HasValue ? s.Goods_Price : int.MaxValue)
                    // 暂时用IsSpecialGoods == 1表示还有货
                                    .Where(s => s.Brand.Brand_Name.Contains(brandName) && s.Goodscategory_.GoodsCategory_S_Name.Contains(smallCategory) && s.IsSpecialGoods == 1)
                                    .Select(s => new MGoods
                                      {
                                          Id = s.Goods_Id,
                                          GoodsName = s.Goods_Name,
                                          GoodsPictureUrl = s.Goods_Pic,
                                          Price = s.Goods_Price.HasValue ? (double)s.Goods_Price : 0,
                                          BrandName = s.Brand.Brand_Name,
                                          GoodsSamllCategoryName = s.Goodscategory_.GoodsCategory_S_Name,
                                          GoodsInfo = s.Goods_info,
                                          GoodsStandard = s.Goods_Standard,
                                          IsSpecialGoods = s.IsSpecialGoods == 1
                                      });
                return query.ToList();
            }
        }

        /// <summary>
        /// 根据商品小分类名称获取全部商品信息
        /// </summary>
        /// <param name="smallCategoryName">商品小分类名称</param>
        /// <returns>商品信息</returns>
        public List<MGoods> GetGoodsInforBySmallCategoryName(string smallCategoryName)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                // IsSpecialGoods == 1表示有货
                var query = db.Goods.Where(g => g.Goodscategory_.GoodsCategory_S_Name == smallCategoryName && g.IsSpecialGoods == 1)
                                  .OrderBy(s => s.Goods_Price.HasValue ? s.Goods_Price : int.MaxValue)
                                  .Select(g => new MGoods
                                              {
                                                  Id = g.Goods_Id,
                                                  GoodsName = g.Goods_Name,
                                                  BrandName = g.Brand.Brand_Name,
                                                  GoodsInfo = g.Goods_info,
                                                  GoodsPictureUrl = g.Goods_Pic,
                                                  Price = !g.Goods_Price.HasValue ? 0 : (double)g.Goods_Price.Value,
                                                  GoodsSamllCategoryName = g.Goodscategory_.GoodsCategory_S_Name,
                                                  GoodsStandard = g.Goods_Standard,
                                                  IsSpecialGoods = g.IsSpecialGoods == 1
                                              });
                return query.ToList();

            }
        }

        /// <summary>
        /// 根据商品ID获取商品详细信息
        /// </summary>
        /// <param name="goodsId">商品ID</param>
        /// <returns>商品信息</returns>
        public MGoods GetGoodsInforByGoodsId(int goodsId)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var g = db.Goods.Single(s => s.Goods_Id == goodsId);
                return new MGoods
                            {
                                Id = g.Goods_Id,
                                GoodsName = g.Goods_Name,
                                BrandName = g.Brand.Brand_Name,
                                GoodsInfo = g.Goods_info,
                                GoodsPictureUrl = g.Goods_Pic,
                                Price = !g.Goods_Price.HasValue ? 0 : (double)g.Goods_Price.Value,
                                GoodsSamllCategoryName = g.Goodscategory_.GoodsCategory_S_Name,
                                GoodsStandard = g.Goods_Standard,
                                IsSpecialGoods = g.IsSpecialGoods == 1
                            };
            }
        }

        public List<MGoods> SearchGoodsByName(string goodsName)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                // IsSpecialGoods == 1 表示有货
                var query = db.Goods.Where(g => g.Goods_Name.Contains(goodsName) && g.IsSpecialGoods == 1)
                                  .Select(g => new MGoods
                                  {
                                      Id = g.Goods_Id,
                                      BrandName = g.Brand.Brand_Name,
                                      GoodsInfo = g.Goods_info,
                                      GoodsName = g.Goods_Name,
                                      GoodsPictureUrl = g.Goods_Pic,
                                      GoodsSamllCategoryName = g.Goodscategory_.GoodsCategory_S_Name,
                                      GoodsStandard = g.Goods_Standard,
                                      IsSpecialGoods = g.IsSpecialGoods == 1,
                                      Price = !g.Goods_Price.HasValue ? 0 : (double)g.Goods_Price.Value,
                                  });
                return query.ToList();
            }
        }

    }
}